package com.fenghongzhang.day008;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 如何定义一个view
 */
public class MyBottomNavigationBar extends FrameLayout implements View.OnClickListener{


    private static final String TAG = "MyBottomNavigationBar";
    private List<MyBottomItem> list;
    private List<View> viewList;
    private LinearLayout.LayoutParams layoutParams;

    private LinearLayout linearLayout;

    private FrameLayout frameLayout;


    //代码
    public MyBottomNavigationBar(@NonNull Context context) {
        super(context);
        init();
    }

    //xml
    public MyBottomNavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        //当前view 的布局属性
        setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));

        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.bar_layout, this, true);

        frameLayout = inflate.findViewById(R.id.container);
        frameLayout.setBackgroundColor(Color.GRAY);

        linearLayout = findViewById(R.id.ll_container);

        //用动态的方式设置xml的属性
        layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.weight =1;
        //初始化容器
        list = new ArrayList<>();
        viewList = new ArrayList<>();

    }

    public MyBottomNavigationBar addItem(MyBottomItem item){
        list.add(item);
        return this;
    }

    public void setBackColor(int color){
        frameLayout.setBackgroundColor(color);
    }


    public void Build(){
        int size = list.size();
        for (int i = 0; i < size; i++) {
            MyBottomItem myBottomItem = list.get(i);
            if(myBottomItem.getTitle() != null){
                TextView textView = new TextView(getContext());
                textView.setText(myBottomItem.getTitle());
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(layoutParams);
                textView.setOnClickListener(this);
                textView.setTag(R.id.itemClick,i);
                textView.setTextColor(getResources().getColor(R.color.unselectTextColor));
                textView.setTextSize(16);
                linearLayout.addView(textView);
                viewList.add(textView);
            }
            if(myBottomItem.getImage() != null){
                ImageView imageView = new ImageView(getContext());
                imageView.setImageDrawable(myBottomItem.getImage());
                imageView.setLayoutParams(layoutParams);
                imageView.setOnClickListener(this);
                imageView.setTag(R.id.itemClick,i);
                linearLayout.addView(imageView);
                viewList.add(imageView);
            }
        }
        setSelectTextColor(0);
    }

    @Override
    public void onClick(View v) {
        int i = (int) v.getTag(R.id.itemClick);
        Log.i(TAG, "onClick: "+i);
        myListener.click(i);

        //写这
        setSelectTextColor(i);

    }


    public void setSelectTextColor(int position){
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            View view = viewList.get(i);
                if(view instanceof TextView){
                    if(position == i) {
                        ((TextView) view).setTextColor(Color.WHITE);
                        ((TextView) view).setTextSize(18);
                        ((TextView) view).setTypeface(Typeface.DEFAULT_BOLD);
                    }else {
                        ((TextView) view).setTextColor(getResources().getColor(R.color.unselectTextColor));
                        ((TextView) view).setTextSize(16);
                }
            }
        }
    }

    public interface MyListener{
        void click(int i);
    }
    private MyListener myListener;

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }
}
